"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const kurento_client_1 = __importDefault(require("kurento-client"));
const defines_1 = require("../lib/defines");
function dissolve(app) {
    const { socket, rooms } = app.locals;
    const { userId } = socket.handshake.query;
    socket.on(defines_1.RequestMethod.iceCandidate, function ({ room, iceCandidate, member }, cb) {
        if (!room)
            return;
        const __room = rooms[room.classId];
        if (!__room) {
            return cb && cb({ status: 'error', message: 'room not find!' });
        }
        const __member = __room.members[member.userId];
        if (!__member) {
            return cb && cb({ status: 'error', message: 'member not find!' });
        }
        const __iceCandidate = kurento_client_1.default.getComplexType('IceCandidate')(iceCandidate);
        const endpoint = __member.getEndpoint(userId);
        if (endpoint) {
            endpoint.addIceCandidate(__iceCandidate).then(cb);
        }
        else {
            __member.candidatesQueue[userId]
                || (__member.candidatesQueue[userId] = []);
            __member.candidatesQueue[userId].push(__iceCandidate);
        }
    });
}
exports.default = dissolve;
